package com.tgg.treeproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tgg.treeproject.pojo.InforationData;
import com.tgg.treeproject.mapper.InforationDataMapper;
import com.tgg.treeproject.service.InforationDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 田国刚
 * @since 2023-04-21
 */
@Service
public class InforationDataServiceImpl extends ServiceImpl<InforationDataMapper, InforationData> implements InforationDataService {
@Resource
InforationDataMapper inforationDataMapper;
    @Override
    public HashMap<String, Object> getInforationData(String id,Integer current,Integer size) {
        QueryWrapper<InforationData> inforationDataQueryWrapper = new QueryWrapper<>();
        inforationDataQueryWrapper.eq("tree_id",id);

        Long aLong = inforationDataMapper.selectCount(inforationDataQueryWrapper);

//        Page<InforationData> page = new Page<>(current,aLong);
//        page.setCurrent(current);
//        page.setSize(size);
//        page.setTotal(aLong);
        List<InforationData> inforationDataPage = inforationDataMapper.getPage(current, size, id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",inforationDataPage);
        map.put("code",200);
        map.put("message","获取成功");
        map.put("totle",aLong);
        return map;
    }

    @Override
    public HashMap<String, Object> saveInforationData(InforationData inforationData) {
        int insert = inforationDataMapper.insert(inforationData);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",insert);
        map.put("code",200);
        map.put("message","插入成功");
        return map;
    }

    @Override
    public HashMap<String, Object> updateInforationData(InforationData inforationData) {
        UpdateWrapper<InforationData> inforationDataUpdateWrapper = new UpdateWrapper<>();
        inforationDataUpdateWrapper.eq("id",inforationData.getId());
        int insert = inforationDataMapper.update(inforationData,inforationDataUpdateWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",insert);
        map.put("code",200);
        map.put("message","更新成功");
        return map;
    }

    @Override
    public HashMap<String, Object> deleteInforationData(String id) {
        UpdateWrapper<InforationData> inforationDataUpdateWrapper = new UpdateWrapper<>();
        inforationDataUpdateWrapper.eq("id",id);
        int delete = inforationDataMapper.delete(inforationDataUpdateWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",delete);
        map.put("code",200);
        map.put("message","删除成功");
        return map;
    }
}
